#include"String.hh"
#include<string.h>

//string()
String ::String()
:_pstr(new char()){} //防止野指针
//string(const char*rhs)
String ::String(const char* rhs)
:_pstr(new char[strlen(rhs)+1])
{
    strcpy(_pstr,rhs);
}
String::String(const String &rhs)
:_pstr(new char[strlen(rhs.c_str())+1])
{
    strcpy(_pstr,rhs.c_str());
}
//~string
String ::~String(){
    if(_pstr)
    {
        delete [] _pstr;
        _pstr = nullptr;
        //cout<<"~"<<endl;
    }
    cout<<"~String"<<endl;
}
//size 
std::size_t String::size() const{
    return strlen(_pstr)-1; //因为构造的时候+1了
}
//c_str
const char* String::c_str() const{
    return _pstr;
}
//operator=
String &String::operator=(const String &rhs)
{
    //考虑自复制
    if(this != &rhs){
        delete [] _pstr;
        _pstr = new char[rhs.size()]();
        strcpy(_pstr,rhs.c_str());
    }
    cout<<"string &operator = (const string&)"<<endl;
    return *this; //返回的是值
}
String &String::operator =(const char *rhs){
    //考虑自复制
    if(_pstr!=rhs)
    {
        delete [] _pstr;
        _pstr = new char[strlen(rhs)+1]();
        strcpy(_pstr,rhs);
    }
    cout<<"string &operator = (const char *rhs)"<<endl;
    return *this; //返回的是值
}

//operator +=
String &String::operator+=(const String &rhs){
    *this = *this + rhs;
    return *this;
}
String &String::operator+=(const char *rhs){
    //string + char*
    *this = *this + rhs;
    return *this;
}

//operator +  //记得friend
String operator+(const String&lhs, const String&rhs)
{
    //应该不改变原来的值
    String temp = lhs;
    strcat(temp._pstr,rhs._pstr);
    return temp;
}
String operator+(const String&lhs, const char *rhs){
    String temp = lhs;
    strcat(temp._pstr,rhs);
    return temp;

}
String operator+(const char *lhs, const String&rhs){
    String temp(lhs);
    strcat(temp._pstr,rhs.c_str());
    return temp;
}

//== 判断字符串是否一样
bool operator==(const String &lhs, const String &rhs)
{
    if(strcmp(rhs._pstr,lhs._pstr) == 0)
    {
        return true;//1
    }
    else return false;
}
//!=
bool operator!=(const String &lhs, const String &rhs)
{
    if((lhs == rhs) == 0) return true;
    else return false;
}
//比较字符串大小
//<
bool operator<(const String &lhs, const String &rhs){
    if(strcmp(rhs._pstr,lhs._pstr) < 0)
    {
        return true;//1
    }
    else return false;
}
//>
bool operator>(const String &rhs, const String &lhs){
    if((rhs<lhs) == 0) return true;
    else return false;
}
//<=
bool operator<=(const String &rhs, const String &lhs){
    if((rhs<lhs)||(rhs == lhs)) return true;
    else return false;
}
//>=
bool operator>=(const String &rhs, const String &lhs){
    if((rhs>lhs)||(rhs == lhs)) return true;
    else return false;
}

char &String::operator[](std::size_t index){
    //注意不能越界访问
    if(index < this->size()-1)
    {
        return _pstr[index];
    }
    else cout<<"out of range"<<endl;
}
//不可以修改下标
const char & String::operator[](std::size_t index) const{
    if(index < this->size()-1){
        return _pstr[index];
    }
    else{  //返回空值
        cout<<"out of range"<<endl;
        static char nullchar = '\0';
        return nullchar;
    }
}

std::ostream &operator<<(std::ostream &os, const String &s){
    os<<s._pstr;
    return os;
}
std::istream &operator>>(std::istream &is, String &s){
    is>>s._pstr;
    return is;
}